package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 首页数据概览
     * Homepage Data Overview
     */
    @Override
    public OverviewVo getOverview() {
        //1.先查redis
        Object dataOverview = redisTemplate.opsForValue().get("Data_OVERVIEW");
        if (dataOverview != null) {
            log.info("缓存有数据,直接返回");
            return (OverviewVo) dataOverview;
        }
        log.info("缓存没有数据,从数据库查询");
//2.如果没有再查数据库
//2.1线索数据概览
        OverviewVo clueOverviewData = clueMapper.getClueOverviewData();
//2.2业务数据概览
        OverviewVo businessOverviewData = businessMapper.getBusinessOverviewData();
//2.3封装数据
        BeanUtils.copyProperties(clueOverviewData, businessOverviewData, "businessTotal", "businessWaitAllot", "businessWaitFollow", "businessFollowing", "businessFalse", "businessConvertCustomer");
//3.缓存redis
        log.info("查数据库,缓存数据到redis");
        redisTemplate.opsForValue().set("Data_OVERVIEW", businessOverviewData,5, TimeUnit.MINUTES);//
        return businessOverviewData;


    }
}
